package vg.modules.notepad;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventCloseAIF;
import vg.services.plugin_manager.event_and_request.event.UIEventCreateNewConnection;
import vg.services.plugin_manager.event_and_request.event.UIEventDeleteConnection;
import vg.services.plugin_manager.event_and_request.request.AUIRequest;
import vg.services.plugin_manager.event_and_request.request.UIRequestGoToInAIF;
import vg.services.plugin_manager.event_and_request.request.UIRequestLoadProject;
import vg.services.plugin_manager.event_and_request.request.UIRequestSaveProject;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AMenuItem;

/**
 * Plugin realizes notepad for reading files. 
 * @author tzolotuhin
 */
public class NotepadPlugin implements IPlugin {
	// Data
	private PluginParameter parameter; // plugin parameter
	// Components
	private JMenuItem jMenuItem;
	private Notepad notepad;
	/**
	 * @see IPlugin
	 */
	public void install(final PluginParameter param) throws Exception {
		this.parameter = param;
		this.notepad = new Notepad(param);
		this.jMenuItem = new JMenuItem("Notepad");
		this.jMenuItem.setIcon(new ImageIcon("./data/resources/textures/notepad/notepad_2.png"));
		this.jMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				notepad.open();
			}
		});
		this.jMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK));
		this.parameter.userInterface.addMenuItem(new AMenuItem(this.jMenuItem) {			
			public void update(Observable o, Object arg) {
				if(arg instanceof AUIEvent) {
					final AUIEvent event = (AUIEvent)arg;
					switch (event.getType()) {
						case DEF_CLOSE_PROGRAM: {
							notepad.dispose();
							break;
						}
						case DEF_CHANGE_UI_STYLE: {
							notepad.updateUITheme();
							break;
						}
						case DEF_CLOSE_AIF: {
							notepad.closeAIF((UIEventCloseAIF)event);
							break;
						}
						case DEF_CREATE_NEW_CONNECTION: {
							notepad.addConnection((UIEventCreateNewConnection)event);
							break;
						}
						case DEF_DELETE_CONNECTION: {
							notepad.deleteConnection((UIEventDeleteConnection)event);
							break;
						}
						case DEF_RESET: {
							notepad.reset();
							break;
						}
					}
				} else if(arg instanceof AUIRequest){
					final AUIRequest request = (AUIRequest)arg;
					switch(request.getType()) {
						case DEF_GOTO_IN_AIF: {
							notepad.goToAIF((UIRequestGoToInAIF)request);
							break;
						}
						case DEF_SAVE_PROJECT: {
							notepad.saveProject(((UIRequestSaveProject)request).getPath());
							break;
						}
						case DEF_LOAD_PROJECT: {
							notepad.loadProject(((UIRequestLoadProject)request).getPath());
							break;
						}
					}
				}
			}
		}, "edit");
	}
}
